<?php

namespace common\behavior\CommonMessagePushing;


use common\model\UserModel;
use common\service\BehaviorService;
use common\service\ProjectStepCategoryConfirmConfigService;
use common\service\ReviewRequestMessageService;
use Webman\Stomp\Client;

/**
 * 延期单待确认 消息通知
 */
class DelayOrderConfirmBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'delay_order' => '',
                'creator_name' => 'strval',
                'step_category_id' => 'intval',
            ],
            [
                'delay_order' => [],
                'creator_name' => '',
                'step_category_id' => 0,
            ],
            'data',
            ['type' => 'delay_order_confirm'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($delayOrder, $creatorName, $stepCategoryId) = $params;
        if (empty($delayOrder) || empty($creatorName) || empty($stepCategoryId)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        if (empty($stepCategoryId) || empty($delayOrder)) {
            return;
        }
        // todo 环节确认人通知
        $reviewerUserIdList = ProjectStepCategoryConfirmConfigService::getInstance()
            ->getStepCategoryConfirmUserIdList($delayOrder['project_id'], $stepCategoryId, 'delay');
        if (empty($reviewerUserIdList)) {
            return;
        }
        $reviewerUserList = model(UserModel::class)->where(['id' => ['IN', $reviewerUserIdList]])->select();
        $messageParam = [
            'first' => '有新的延期单待审批',
            'delay_order_name' => $delayOrder['name'],
            'sender_name' => $creatorName,
            'remark' => '请及时审批',
            'delay_order_id' => $delayOrder['id'],
            'project_id' => $delayOrder['project_id'],
        ];

        $messageTemplateId = ReviewRequestMessageService::getInstance()->delayOrderConfirmMessageTemplateId();
        $messageGenerated = [];
        foreach ($reviewerUserList as $reviewerUser) {
            $messageGenerated[] = [
                "param" => $messageParam,
                'receiver' => $reviewerUser['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'is_popup' => 1,
                'message_key' => 'task_delay_order_need_review',
            ];
        }

        /***********  发送新的队列  ************/
        foreach ($messageGenerated as $message) {
            Client::send('common_message_pushing', build_queue_data_with_xu([
                'message_generated' => [$message],
            ]));
        }
        /***********  结束  ************/
    }
}
